﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Mathmall.Administration.Migrations
{
    /// <inheritdoc />
    public partial class AddUserTable : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterColumn<string>(
                name: "FirstLetter",
                table: "Brand",
                type: "char(5)",
                nullable: true,
                comment: "检索首字母",
                oldClrType: typeof(string),
                oldType: "char",
                oldNullable: true,
                oldComment: "检索首字母");

            migrationBuilder.CreateTable(
                name: "User",
                columns: table => new
                {
                    UserId = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    UserName = table.Column<string>(type: "varchar(255)", nullable: false, comment: "用户名称"),
                    Password = table.Column<string>(type: "varchar(255)", nullable: false, comment: "用户密码"),
                    Description = table.Column<string>(type: "varchar(255)", nullable: true, comment: "用户描述"),
                    Avatar = table.Column<string>(type: "varchar(255)", nullable: true, comment: "用户头像"),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "创建时间"),
                    LastUpdateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "最后更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("UserId", x => x.UserId);
                },
                comment: "用户基础信息表");
            migrationBuilder.Sql(
                @"
                    IF EXISTS(SELECT * FROM SYS.TRIGGERS WHERE NAME = 'trg_UserCreateTime')
                    BEGIN
	                    DROP TRIGGER trg_UserCreateTime
                    END
                    GO
                    CREATE TRIGGER trg_UserCreateTime
                    ON [User]
                    AFTER INSERT
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE [User]
	                    SET CreateTime = GETDATE()
	                    WHERE UserID IN (SELECT UserId FROM inserted);
                    END;

                    IF EXISTS(SELECT * FROM SYS.TRIGGERS WHERE NAME = 'trg_UserLastUpdateTime')
                    BEGIN
	                    DROP TRIGGER trg_UserLastUpdateTime
                    END
                    GO
                    CREATE TRIGGER trg_UserLastUpdateTime
                    ON [User]
                    AFTER UPDATE
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE [User]
	                    SET LastUpdateTime = GETDATE()
	                    WHERE UserID IN (SELECT UserId FROM inserted);
                    END;
                ");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "User");

            migrationBuilder.AlterColumn<string>(
                name: "FirstLetter",
                table: "Brand",
                type: "char",
                nullable: true,
                comment: "检索首字母",
                oldClrType: typeof(string),
                oldType: "char(5)",
                oldNullable: true,
                oldComment: "检索首字母");
        }
    }
}
